<template>
  <div>
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane :label="item.name" :name="item.id" v-for="item in tabList" :key="item.id"></el-tab-pane>
    </el-tabs>
    <div class="search-list mb-10">
      <el-button type="primary" @click="editCate('add', null)" size="small" plain><i
          class="el-icon-plus"></i>添加巡班项
      </el-button>
    </div>
    <div class="table-line-body mt-10 yangTable">
      <el-table
          :data="treeList"
          row-key="id"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="name" label="巡班项" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column width="50" align="center" label="序号">
          <template slot-scope="scope">
            <span v-if="!scope.row.parent_id">{{scope.row.sort}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" align="center" width="120" label="巡班类型" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.parent_id">
              {{typelistOption[scope.row.type]}}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="name" width="120" align="center" label="巡班对象" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.parent_id">{{
                userList[scope.row.user_type] ? userList[scope.row.user_type].name : ''
              }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="editCate('edit', scope.row)"
            >修改
            </el-button>
            <el-button
                v-if="!scope.row.parent_id"
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="editCate('add', scope.row)"
            >新增
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="deletefn(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="height40"></div>
    <patrol :visible.sync="patrolVisible" :optionData="optionData" @InitFn="InitFn"
            :selectList="selectList" :activeName="activeName" :optionType="optionType"></patrol>
  </div>
</template>

<script>
import {pagination} from '@/utils/util.js'
import patrol from "../components/patrol";
import {classDynamiceIndex, classDynamiceDelete} from "@/api/admin/ClassDynamics"
import dataMenu from "../training/dataMenu.js"

export default {
  components: {
    patrol
  },
  data() {
    return {
      tableStripe: true, // 是否斑马栏
      tableBorder: true, // 单元格右边边框
      arryList: [], // 数据列表
      patrolVisible: false,
      // 页数
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      // 搜索
      searchForm: {},
      checkTab: '',
      optionData: null,
      searchBox: {
        class_id: '',
        section_id: ''
      },
      tabList: [
        {
          name: '行政',
          id: '1'
        },
        {
          name: '后勤',
          id: '2',
        },
        {
          name: '业务',
          id: '3',
        }
      ],
      userList: {
        1: {
          name: '幼儿',
          id: 1
        },
        2: {
          name: '教师',
          id: 2
        },
        4: {
          name: '生活老师',
          id: 4
        },
        3: {
          name: '场地',
          id: 3
        }
      },
      activeName: '1',
      optionType: '',
      getPower: {
        'section_id': {
          name: 'section_id', // key名称
          default: '', // 默认值
          propertyType: "Select", // 组件名称
          placeholder: '年段', // 默认值
          valueText: '', // 值
          leve: 2,
          api: this.api.user_sectionindex, // 下拉框的请求接口
          keyName: 'section_name',// 调用的字段
          child: [
            {
              name: 'class_id',
              default: '',
              propertyType: "",
              placeholder: '班级',
              valueText: '',
              api: this.api.user_classesindex,
              keyName: 'class_name',
            }
          ]
        }
      },
      treeList: [],
      isExpandAll: false,
      selectList: [],
      typelistOption: {
        1: '班级',
        2: '公共区域',
        3: '通用'
      },
    }
  },
  watch: {
    activeName() {
      console.log('变化了')
      this.InitFn()
    }
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    async InitFn() {
      let option_ = JSON.parse(JSON.stringify(this.searchBox))
      option_.p = 0
      option_.department_id = this.activeName
      classDynamiceIndex(option_).then(res => {
        console.log('res:', res)
        if (!res.code) {
          this.treeList = this.$handleTree(res.list, 'id', 'parent_id');
          let list = this.$handleTree(res.list, 'id', 'parent_id')
          list.unshift({id: 0, name: '主类目'}) // dataMenu.data
          this.selectList = list
          // let list_ = []
          // for(let item of dataMenu.data){
          //   item.children = []
          //   for(let row of list){
          //     if(row.department_id===item.department_id){
          //       row.parent_id = item.id
          //       item.children.push(row)
          //     }
          //   }
          //   list_.push(item)
          // }
          // this.treeList = list_
          // this.arryList = res.list.data;
          this.page.total = res.list.total;
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    handleSelectionChange(val) {
      this.handleSelection = val
    },
    deletefn(row) {
      this.$confirm(`确认删除【${row.name}】, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(row.id)
      }).catch(() => {
      })
    },
    async detelRespon(id) {
      classDynamiceDelete({id: id}).then(res => {
        if (!res.code) {
          this.$message.success(res.msg)
          this.InitFn()
        }
      })
    },
    editCate(type, row) {
      this.optionType = type
      this.optionData = row
      this.patrolVisible = true
    }
  },
  mounted() {
  },
}
</script>
<style>
.lead-list {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fddeae;
  margin-bottom: 5px;
  border-radius: 2px;
  padding: 5px 0;
}

.lead-list:last-child {
  margin-bottom: 0;
}

.lead-list span {
  margin-right: 30px;
}
</style>

